package com.wenhai.leetcode.search;

import java.util.Arrays;

/**
 *https://leetcode-cn.com/problems/koko-eating-bananas/
 *
 * @author wenhai
 * @date   2021/3/4
 */
public class _875爱吃香蕉的珂珂 {

    public int minEatingSpeed(int[] piles, int h) {
        int l = 1;
        int r = Arrays.stream(piles).max().getAsInt();
        while (l < r) {
            int mid = l + ((r - l) >>> 1);
            if (eatingTime(piles, mid) <= h) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        System.gc();
        return l;
    }

    private int eatingTime(int[] piles, int k) {
        int time = 0;
        for(int pile: piles){
            time += pile / k + (pile % k > 0 ? 1 : 0);
        }
        return time;
    }

    public static void main(String[] args) {
        int[] piles = {3, 6, 7, 11};
        new _875爱吃香蕉的珂珂().minEatingSpeed(piles, 8);
    }
}
